package cn.fintecher.pangolin.entity.mysql;

import cn.fintecher.pangolin.common.enums.AccountType;
import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.enums.UserStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Set;

/**
 * @Auther: xiaqun
 * @Description: 用户实体
 * @Date: 9:29 2019/8/13
 */

@Data
@Entity
@Table(name = "user")
@ApiModel(value = "user", description = "用户实体")
public class User extends BaseEntity {
    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "姓名")
    private String realName;

    @ApiModelProperty(value = "是否管理员")
    @Enumerated(EnumType.STRING)
    private BaseSelect isManage;

    @ApiModelProperty(value = "状态")
    @Enumerated(EnumType.STRING)
    private UserStatus status;

    @ApiModelProperty(value = "职务")
    private String station;

    @ApiModelProperty(value = "账户类型")
    @Enumerated(EnumType.STRING)
    private AccountType accountType;

    @ApiModelProperty(value = "锁定次数")
    private Integer lockNumber;

    @ApiModelProperty(value = "所属机构")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "depart_id")
    private Department department;

    @ApiModelProperty(value = "用户详情信息")
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_detail_id")
    private UserDetail userDetail;

    @ApiModelProperty(value = "角色集合")
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

    @ApiModelProperty(value = "用户头像url")
    private String url;

    @ApiModelProperty(value = "创建人ID")
    private String operatorId;

    @ApiModelProperty(value = "创建时间")
    private Date operateTime;

    @ApiModelProperty(value = "更新人ID")
    private String updaterId;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "是否开启合规检查")
    @Enumerated(EnumType.STRING)
    private BaseSelect isCheck;

    @ApiModelProperty(value = "通道号")
    private String channelNo;

    @ApiModelProperty(value = "是否系统用户")
    @Enumerated(EnumType.STRING)
    private BaseSelect isSystem = BaseSelect.NO;

    /**
     * 处理创建人、更新人、创建时间、更新时间
     *
     * @param id   用户ID
     * @param date 时间
     * @param b    标识 true时设置创建人和创建时间
     */
    public void process(String id, Date date, boolean b) {
        if (b) {
            this.operatorId = id;
            this.operateTime = date;
        }
        this.updaterId = id;
        this.updateTime = date;
    }
}
